<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogTaskBackNodesInChild"
    title="发起流程"
    width="800px"
  >
    <el-radio-group v-model="backNode" @change="selectBackNode">
      <el-radio v-for="item in backNodes" border :label="item">
        【{{ item.nodeId }}-{{ item.nodeName }}】{{ item.userName }}
      </el-radio>
    </el-radio-group>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button
        v-permission="['admin', 'instance:add']"
        :loading="submitLoading"
        type="primary"
        @click="doSubmit"
      >确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import crud from '@/mixins/crud'
import crudTask from '@/api/flowable/task'

export default {
  name: 'TaskBackNodes',
  components: {},
  mixins: [crud],
  props: {
    visible: {
      type: Boolean,
      default: function() {
        return false
      }
    },
    executeTaskId: {
      type: String,
      default: function() {
        return ''
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      backNode: '',
      backNodes: []
    }
  },
  computed: {
    dialogTaskBackNodesInChild: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    beforeInit() {
      crudTask.backNodes(this.executeTaskId)
        .then((res) => {
          const data = res
          this.backNodes = data
          this.backNode = this.backNodes[0]
        })
        .catch(err => {
          console.log(err.response.data.message)
        })
    },
    cancel() {
      this.dialogStartProcessVisibleInChild = false
    },
    doSubmit() {
      this.dialogTaskBackNodesInChild = false
      this.$emit('backTaskFinished', this.backNode)
    },
    selectBackNode(val) {
      this.backNode = val
    }
  }
}
</script>
